package com.lzm.ydpt.view.pop;

import android.animation.ObjectAnimator;
import android.content.Context;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.byt.framlib.commonutils.DisplayUtil;
import com.lzm.ydpt.R;

import java.util.ArrayList;

public class UserStateCommonPop extends PopupWindow {

    private Context mContext;
    // 列表弹窗的间隔
    protected final int LIST_PADDING = 10;
    // 实例化一个矩形
    private Rect mRect = new Rect();
    // 坐标的位置（x、y）
    private final int[] mLocation = new int[2];
    // 屏幕的宽度和高度
    private int mScreenWidth, mScreenHeight;
    // 判断是否需要添加或更新列表子类项
    private boolean mIsDirty;
    // 位置不在中心
    private int popupGravity = Gravity.NO_GRAVITY;
    // 弹窗子类项选中时的监听
    private OnItemOnClickListener mItemOnClickListener;
    // 定义列表对象
    private LinearLayout ll_common_list_popview;
    private ListView mListView;

    private BaseAdapter mAdapter;

    // 定义弹窗子类项列表
    private ArrayList<String> mAction = new ArrayList<>();

    public UserStateCommonPop(Context mContext, int width, int height, OnItemOnClickListener listener) {
        super(mContext);
        this.mContext = mContext;
        this.mItemOnClickListener = listener;
        setContentView(LayoutInflater.from(mContext).inflate(R.layout.common_list_popview, null));
        // 设置弹窗的宽度和高度
        setWidth(width);
        setHeight(height);
        setFocusable(false);
        // 设置弹窗内可点击
        setTouchable(true);
        // 设置弹窗外可点击
        setOutsideTouchable(true);
        // setCanceledOnTouchOutside(false);
        setBackgroundDrawable(new BitmapDrawable());
        update();
        // 获得屏幕的宽度和高度
        mScreenWidth = DisplayUtil.getScreenWidth(mContext);
        mScreenHeight = DisplayUtil.getScreenHeight(mContext);
        // 设置弹窗的布局界面
        initUI();
    }

    /**
     * 初始化弹窗列表
     */
    private void initUI() {
        // getContentView这种设置不错
        mListView = getContentView().findViewById(R.id.common_listview);
        ll_common_list_popview = getContentView().findViewById(R.id.ll_common_list_popview);
        mListView.setVerticalScrollBarEnabled(true);
        mListView.setOnItemClickListener((arg0, arg1, index, arg3) -> {
            if (mItemOnClickListener != null) {
                mItemOnClickListener.onItemClick(mAction.get(index),index);
            }
            dismiss();
        });
        populateActions();
    }

    /**
     * 设置弹窗列表子项
     */
    private void populateActions() {
        // 设置列表的适配器
        mAdapter = new BaseAdapter() {
            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                PopHolder popHolder;
                if (convertView == null) {
                    popHolder = new PopHolder();
                    convertView = View.inflate(mContext, R.layout.item_pop_action, null);
                    popHolder.actionInfo = convertView.findViewById(R.id.action_info);
                    convertView.setTag(popHolder);
                } else {
                    popHolder = (PopHolder) convertView.getTag();
                }
                // 设置文本文字
                popHolder.actionInfo.setText(mAction.get(position));
                return convertView;
            }

            @Override
            public long getItemId(int position) {
                return position;
            }

            @Override
            public String getItem(int position) {
                return mAction.get(position);
            }

            @Override
            public int getCount() {
                return mAction.size();
            }
        };
        mListView.setAdapter(mAdapter);
    }

    class PopHolder {
        TextView actionInfo;
    }

    /**
     * 添加子类项
     */
    public void addAction(String action) {
        if (action != null) {
            mAction.add(action);
            mIsDirty = true;
        }
    }

    /**
     * 清除子类项
     */
    public void cleanAction() {
        if (!mAction.isEmpty()) {
            mAction.clear();
            mIsDirty = true;
        }
    }

    /**
     * 根据位置得到子类项
     */
    public String getAction(int position) {
        if (position < 0 || position > mAction.size()) return null;
        return mAction.get(position);
    }

    public void showViewDown(View view, ImageView img) {
        // 显示弹窗的位置
        if (!isShowing() && img != null) {
            showMoreH(img);
        }
        setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                if (img != null) {
                    showMoreV(img);
                }
            }
        });
        showAsDropDown(view);
    }

    public void setHoneDetailsUi() {
        cleanAction();
        addAction("接单中");
        addAction("休息中");
        mAdapter.notifyDataSetChanged();
    }

    /**
     * 显示弹窗列表界面
     * x是View的宽度
     * 在View控件下面的
     */
    public void showAsDropDown(View view, ImageView img, int xoff) {
        // 显示弹窗的位置
        if (!isShowing() && img != null) {
            showMoreH(img);
        }
        setOnDismissListener(new OnDismissListener() {
            @Override
            public void onDismiss() {
                if (img != null) {
                    showMoreV(img);
                }
            }
        });
        showAsDropDown(view, mScreenWidth - xoff, 0);
    }

    public void showAsDropDown(View view, ImageView img) {
        // 显示弹窗的位置
        if (!isShowing() && img != null) {
            showMoreH(img);
        }
        setOnDismissListener(new OnDismissListener() {
            @Override
            public void onDismiss() {
                if (img != null) {
                    showMoreV(img);
                }
            }
        });
        showAsDropDown(view);
    }

    private void showMoreH(ImageView image_right) {
        ObjectAnimator animator = ObjectAnimator.ofFloat(image_right, "rotation", 0f, 180f);
        animator.setDuration(500);
        animator.start();
    }

    private void showMoreV(ImageView image_right) {
        ObjectAnimator animator = ObjectAnimator.ofFloat(image_right, "rotation", 180f, 360f);
        animator.setDuration(500);
        animator.start();
    }

    public interface OnItemOnClickListener {
        void onItemClick(String item,int index);
    }
}
